﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KZ400B.Controls
{

    public delegate void DataChangedDelegate (object sender);

    /// <summary>
    /// NumeralControl.xaml 的交互逻辑
    /// </summary>
    public partial class NumeralControl : UserControl
    {
        public static DependencyProperty InputWidthProperty = DependencyProperty.Register(
"InputWidth", typeof(int)
, typeof(NumeralControl)
, new PropertyMetadata((int)int.MinValue, new PropertyChangedCallback(changeContentSize))
);
        static void changeContentSize(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NumeralControl control = (NumeralControl)d;
            control.cmbData.Width = control.InputWidth;
            control.txtData.Width = control.InputWidth;
        }

        public int InputWidth
        {
            get { return (int)GetValue(InputWidthProperty); }
            set { SetValue(InputWidthProperty, value); }
        }

        public NumeralControl()
        {
            InitializeComponent();
        }

        public event DataChangedDelegate DataChanged;


        private string m_data;

        public string Data
        {
            get
            {
                if (string.IsNullOrEmpty(m_data)) { m_data = "0"; }
                if (cmbData.Visibility == System.Windows.Visibility.Visible)
                {
                    m_data = cmbData.SelectedItem.ToString();
                }
                else
                {
                    m_data = txtData.Text;
                }
                return m_data;
            }
        }

        private void btnInput_Click(object sender, RoutedEventArgs e)
        {
            if (cmbData.Visibility == System.Windows.Visibility.Visible)
            {
                cmbData.Visibility = System.Windows.Visibility.Collapsed;
                txtData.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                cmbData.Visibility = System.Windows.Visibility.Visible;
                txtData.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        public System.Collections.IEnumerable ItemsSource
        {
            get { return cmbData.ItemsSource; }
            set { cmbData.ItemsSource = value; }
        }

        public int SelectedIndex
        {
            get { return cmbData.SelectedIndex; }
            set { cmbData.SelectedIndex = value; }
        }

        public object SelectedItem
        {
            get { return Data; }
            set
            {
                if (cmbData.Visibility == System.Windows.Visibility.Visible)
                {
                    cmbData.SelectedItem = value;
                }
                else
                {
                    txtData.Text = value.ToString();
                }
                
            }
        }

        public void BindValue(int value)
        {
            cmbData.Visibility = System.Windows.Visibility.Collapsed;
            txtData.Visibility = System.Windows.Visibility.Visible;
            txtData.Text = value.ToString();
        }

        public void BindValue(double value)
        {
            cmbData.Visibility = System.Windows.Visibility.Collapsed;
            txtData.Visibility = System.Windows.Visibility.Visible;
            if (value == 0)
            {
                txtData.Text = "0.000";
            }
            else
            {
                txtData.Text = value.ToString("0.000");
            }
        }

        private void cmbData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DataChanged != null)
            {
                DataChanged(this);
            }
        }

        private void txtData_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (DataChanged != null)
            {
                DataChanged(this);
            }
        }
    }
}
